//
//  MainViewController.swift
//  Weobo
//
//  Created by 史亚洲 on 16/1/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit


class MainViewController: UITabBarController {
    
    //注意按钮点击事件的调用是有运行循环监听的,并且以消息机制传递的,因此按钮监听不能单纯设置为private,否则必须添加@objc
    @objc private func  plusBtnClick(){
        print(__FUNCTION__)
        let compose=ComposeViewController()
        let nav=UINavigationController(rootViewController: compose)
        presentViewController(nav, animated: true, completion: nil)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置自定义tabBar,需要使用kvc模式赋值
        let mainTabBar = MainTabBar()
        setValue(mainTabBar, forKey: "tabBar")
        
        //给加号按钮添加点击事件
        mainTabBar.plusBtn.addTarget(self, action: "plusBtnClick", forControlEvents: .TouchUpInside)
        
        addChildViewControllers()
        
    }
    
    //2.设置视图控制器,私有的,不想被外界修改
    private func addChildViewControllers() {
        //2.1首页
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home",index: 0)
        
        //2.2消息
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center",index: 1)
        
        //2.3发现
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover",index: 2)
        
        //2.4我
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile",index: 3)
        
    }
    
    
    //1.重构代码抽取参数:设置标题,图片,控制器;私有的,不想被外界修改
    private func addChildViewController(vc: UIViewController,title: String,imageName: String,index: Int) {
        
        //1.1设置标题
        vc.title = title
        
        //1.2设置图片
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.tag = index
        //1.3设置选中后的tabBar颜色
       // self.tabBar.tintColor = UIColor.orangeColor()
        
        //1.4设置导航控制器
        let nav = BaseNavViewController(rootViewController: vc)
        addChildViewController(nav)
        
        
    }
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        print(item.tag)
        
        //获取点击的item对应的tabBarButton
        var index=0
        for subView in tabBar.subviews{
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){
                if index==item.tag{
                    //确定点击的是哪一个UITabBarButton
                    for v in subView.subviews{
                        if v.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!){
                            print(v)
                            //执行动画效果
                            v.transform=CGAffineTransformMakeScale(0.6, 0.6)
                            UIView.animateWithDuration(0.25, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 2, options: [], animations: { () -> Void in
                                v.transform=CGAffineTransformIdentity
                                
                                }, completion: { (_) -> Void in
                                    print("OK")
                            })
                            
                            
                            
                        }
                    }
                    
                }
                index++
                
                
            }
            
            
            
        }
        
        
    }
    
    
}
